﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


/// <summary>
/// Data Access Layer to deal with Package table
/// </summary>
public class DALPackage : BaseDAL
{

    public DALPackage()
    {
        //    db = new MailDistributionSystemDBDataContext();
    }

    public void insertPackage(tblPackage package)
    {
        db.tblPackages.InsertOnSubmit(package);
        db.SubmitChanges();
    }

    public void updatePackage(tblPackage package)
    {
        db.SubmitChanges();
    }

    public void updatePackage()
    {
        db.SubmitChanges();
    }

    public tblPackage getPackage(int id)
    {
        return db.tblPackages.SingleOrDefault(c => c.ID == id);
    }

    public tblPackage getPackage(string barcode)
    {
        return db.tblPackages.SingleOrDefault(c => c.Barcode == barcode);
    }



    internal List<vPackageDetail> ListPackages()
    {
        return db.vPackageDetails.ToList();
    }

    internal List<vPackageDetail> ListPackages(DateTime dt)
    {
        return db.vPackageDetails.Where(c => c.PackageDate.Value.Date == dt.Date).ToList();
    }

    internal List<vPackageDetail> ListPackages(int year, int month)
    {
        DateTime dt = new DateTime(year, month, 1);
        DateTime dtnext = dt.AddMonths(1).AddDays(-1);

        return db.vPackageDetails.Where(c => c.PackageDate.Value >= dt && c.PackageDate.Value <= dtnext).ToList();
    }

    internal vPackageDetail GetvPackage(string barcode)
    {
        return db.vPackageDetails.SingleOrDefault(c => c.Barcode == barcode);
    }

    internal vPackageDetail GetvPackageDetail(int packageid)
    {
        return db.vPackageDetails.SingleOrDefault(c => c.ID == packageid);
    }

    internal List<vPackageDetail> GetvPackagesByEmail(string email)
    {
        return db.vPackageDetails.Where(c => c.SenderEmail == email).ToList();
    }

    internal List<vPackageDetail> GetvProgressPackagesByEmail(string email)
    {
        return db.vPackageDetails.Where(c => c.SenderEmail == email && c.Status != "Completed").ToList();
    }

    internal List<vPackageDetail> GetvCompletedPackagesByEmail(string email)
    {
        return db.vPackageDetails.Where(c => c.SenderEmail == email && c.Status == "Completed").ToList();
    }

}